package com.debai.service.Impl;

import com.debai.mapper.UserMapper;
import com.debai.pojo.Order;
import com.debai.pojo.Position;
import com.debai.pojo.User;
import com.debai.service.OrderService;
import com.debai.service.PositionService;
import com.debai.service.UserService;
import com.debai.util.JwtUtil;
import com.debai.util.Md5Util;
import com.debai.util.PageBean;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionService positionService;

    @Override
    public int insert(User user,int positionId,String token) {
        user.setPositionId(positionId);
        Map<String, Object> map = JwtUtil.parseToken(token);
        int adminId = (int) map.get("adminId");
        user.setAdminId(adminId);
//        user.setUserPassword(Md5Util.getMD5String(user.getUserPassword()));
        int i = userMapper.insert(user);
        return i;
    }

    @Override
    public List<User> selectAll() {
        List<User> users=userMapper.selectAll();
        users.forEach(user -> {
            int positionId =user.getPositionId();
            Position position = positionService.selectById(positionId);
            user.setPosition(position);
        });
        return users;
    }

    @Override
    public int delete(int userId) {
        int i = userMapper.delete(userId);
        return i;
    }

    @Override
    public User selectByAccountAndPassword(String userName, String userPassword) {
        User user=userMapper.selectByAccountAndPassword(userName,userPassword);
        return user;
    }

    @Override
    public User selectById(Integer userId) {
        User user=userMapper.selectById(userId);
        return user;
    }

    @Override
    public User selectByName(String userName) {
        User user=userMapper.selectByName(userName);
        return user;
    }

}
